﻿namespace AgileZen.Tests
{
    using System;
    using System.Text;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using AgileZen.Models;

    [TestClass]
    public class AgileZenServiceTest
    {
        private const int _projectid = 19451;
        private const int _storyid = 1;
        private const int _roleid = 41481;
        private static IAgileZenService _service;

        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
            _service = new AgileZenService();    
        }


        [TestMethod]
        public void AgileZenService_Can_Get_All_Projects()
        {
            var service = new AgileZenService();
            var projects = service.GetProjects();

            Assert.IsNotNull(projects, "GetProjects did not return a result");
            Assert.IsFalse(projects.Page == 0, "GetProjects did return a result, but no Page");
            Assert.IsFalse(projects.PageSize == 0, "GetProjects did return a result, but no PageSize");
            Assert.IsFalse(projects.TotalItems == 0, "GetProjects did return a result, but no TotalItems");
            Assert.IsFalse(projects.TotalPages == 0, "GetProjects did return a result, but no TotalPages");
            Assert.IsFalse(projects.Items.Count() == 0, "GetProjects did return a result, but no projects");
        }

        [TestMethod]
        public void AgileZenService_Can_Get_Project_By_Id()
        {
            var service = new AgileZenService();
            var project = service.GetProject(_projectid);

            Assert.IsNotNull(project, "GetProject did not return a result");
            
        }

        [TestMethod]
        public void AgileZenService_Can_Update_Project()
        {
            var service = new AgileZenService();
            var project = service.GetProject(_projectid);
            project.Name = "Promptus Agile Board";

            service.UpdateProject(project.Id, ref project);

            Assert.IsNotNull(project, "GetProject did not return a result");

        }

        [TestMethod]
        public void AgileZenService_Can_Get_Project_By_Id_With_Details()
        {
            var service = new AgileZenService();
            var project = service.GetProject(_projectid, Project.Enrichments.Details);

            Assert.IsNotNull(project, "GetProject did not return a result");
            Assert.IsNotNull(project.Details, "GetProject did return a result, but no Details");

        }

        [TestMethod]
        public void AgileZenService_Can_Get_Project_By_Id_With_Phases()
        {
            var service = new AgileZenService();
            var project = service.GetProject(_projectid, Project.Enrichments.Phases);

            Assert.IsNotNull(project, "GetProject did not return a result");
            Assert.IsFalse(project.Phases.Count() == 0, "GetProjects did return a result, but no projects");
         }

        [TestMethod]
        public void AgileZenService_Can_Get_Project_By_Id_With_Members()
        {
            var service = new AgileZenService();
            var project = service.GetProject(_projectid, Project.Enrichments.Members);

            Assert.IsNotNull(project, "GetProject did not return a result");
            Assert.IsFalse(project.Members.Count() == 0, "GetProjects did return a result, but no projects");
        }

        [TestMethod]
        public void AgileZenService_Can_Get_Project_By_Id_With_Roles()
        {
            var service = new AgileZenService();
            var project = service.GetProject(_projectid, Project.Enrichments.Roles);

            Assert.IsNotNull(project, "GetProject did not return a result");
            Assert.IsFalse(project.Roles.Count() == 0, "GetProjects did return a result, but no projects");
        }

        [TestMethod]
        public void AgileZenService_Can_Get_Project_By_Id_With_Invites()
        {
            var service = new AgileZenService();
            var project = service.GetProject(_projectid, Project.Enrichments.Invites);

            Assert.IsNotNull(project, "GetProject did not return a result");
            //can;t assert, no invites with a free account
            //Assert.IsFalse(project.Invites.Count() == 0, "GetProjects did return a result, but no projects");
        }

        [TestMethod]
        public void AgileZenService_Can_Get_Project_By_Id_With_Metrics()
        {
            var service = new AgileZenService();
            var project = service.GetProject(_projectid, Project.Enrichments.Metrics);

            Assert.IsNotNull(project, "GetProject did not return a result");
            //can;t assert, no invites with a free account
            //Assert.IsFalse(project.Invites.Count() == 0, "GetProjects did return a result, but no projects");
        }

        [TestMethod]
        public void AgileZenService_Can_Get_Project_By_Id_With_MultipleEnrichments()
        {
            var service = new AgileZenService();
            var project = service.GetProject(_projectid,
                                             Project.Enrichments.Details,
                                             Project.Enrichments.Phases);

            Assert.IsNotNull(project, "GetProject did not return a result");

        }

        [TestMethod]
        public void AgileZenService_Can_Get_All_Stories_By_ProjectId()
        {
            var service = new AgileZenService();
            var stories = service.GetStories(_projectid);

            Assert.IsNotNull(stories, "GetProjects did not return a result");
            Assert.IsFalse(stories.Page == 0, "GetProjects did return a result, but no Page");
            Assert.IsFalse(stories.PageSize == 0, "GetProjects did return a result, but no PageSize");
            Assert.IsFalse(stories.TotalItems == 0, "GetProjects did return a result, but no TotalItems");
            Assert.IsFalse(stories.TotalPages == 0, "GetProjects did return a result, but no TotalPages");
            Assert.IsFalse(stories.Items.Count() == 0, "GetProjects did return a result, but no projects");
        }

        [TestMethod]
        public void AgileZenService_Can_Get_Story_By_ProjectId_And_StoryId()
        {
            var service = new AgileZenService();
            var story = service.GetStory(_storyid, _projectid);

            Assert.IsNotNull(story, "GetProjects did not return a result");
           
        }

        [TestMethod]
        public void AgileZenService_Can_Get_Story_By_ProjectId_And_StoryId_With_Details()
        {
            var service = new AgileZenService();
            var story = service.GetStory(_storyid, _projectid, Story.Enrichments.Details);

            Assert.IsNotNull(story, "GetProjects did not return a result");
            Assert.IsNotNull(story.Details, "GetProjects did not return a result");
        }

        [TestMethod]
        public void AgileZenService_Can_Get_Story_By_ProjectId_And_StoryId_With_Tags()
        {
            var service = new AgileZenService();
            var story = service.GetStory(_storyid, _projectid, Story.Enrichments.Tags);

            Assert.IsNotNull(story, "GetProjects did not return a result");
            Assert.IsFalse(story.Tags.Count == 0, "GetProjects did not return a result");
        }

        [TestMethod]
        public void AgileZenService_Can_Get_Story_By_ProjectId_And_StoryId_With_Tasks()
        {
            var service = new AgileZenService();
            var story = service.GetStory(_storyid, _projectid, Story.Enrichments.Tasks);

            Assert.IsNotNull(story, "GetProjects did not return a result");
            Assert.IsFalse(story.Tasks.Count == 0, "GetProjects did not return a result");
        }

        [TestMethod]
        public void AgileZenService_Can_Get_Roles_By_ProjectId()
        {
            var service = new AgileZenService();
            var roles = service.GetRoles(_projectid);

            Assert.IsNotNull(roles, "GetRoles did not return a result");
            Assert.IsFalse(roles.Items.Count == 0, "GetProjects did not return a result");
        }

        [TestMethod]
        public void AgileZenService_Can_Get_Role_By_ProjectId_And_RoleId()
        {
            var service = new AgileZenService();
            var role = service.GetRole(_projectid, _roleid);

            Assert.IsNotNull(role, "GetRole did not return a result");
            //Assert.IsFalse(roles.Items.Count == 0, "GetProjects did not return a result");
        }

        [TestMethod]
        public void AgileZenService_Can_CreateUpdateDeleteRole()
        {
            var service = new AgileZenService();
            var role = new Role() { Name = "PMO" };

            service.CreateRole(_projectid, ref role);

            Assert.IsFalse(role.Id == 0, "Role id not assigned");
            Assert.IsTrue(role.Name == "PMO", "Name is not stored correctly");

            role.Name = "Project Management Organization";
            service.UpdateRole(_projectid, ref role);

            Assert.IsTrue(role.Name == "Project Management Organization", "Name is not stored correctly");

            service.DeleteRole(_projectid, role.Id);
        }

        [TestMethod]
        public void AgileZenService_Can_CreateUpdateDeleteTask()
        {
            var task = new Task() { Text = "PMO" };

            _service.CreateTask(_storyid, _projectid, ref task);

            Assert.IsFalse(task.Id == 0, "Role id not assigned");
            Assert.IsTrue(task.Text == "PMO", "Name is not stored correctly");

            task.Text = "Project Management Organization";
            _service.UpdateTask(_storyid, _projectid, ref task);

            Assert.IsTrue(task.Text == "Project Management Organization", "Name is not stored correctly");

            _service.DeleteTask(_storyid, _projectid, task.Id);
        }

        [TestMethod]
        public void AgileZenService_Can_CreateUpdateDeleteTag()
        {
            var tag = new Tag() { Name = "PMO" };

            _service.CreateTag(_storyid, _projectid, ref tag);

            Assert.IsFalse(tag.Id == 0, "Tag id not assigned");
            Assert.IsTrue(tag.Name == "PMO", "Name is not stored correctly");

            tag.Name = "Project Management Organization";
            _service.UpdateTag(_storyid, _projectid, ref tag);

            Assert.IsTrue(tag.Name == "Project Management Organization", "Name is not stored correctly");

            //_service.DeleteTag(_storyid, _projectid, tag.Id);
        }

        [TestMethod]
        public void AgileZenService_Can_CreateUpdateDeletePhase()
        {
            var role = new Role() { Name = "PMO" };

            _service.CreateRole(_projectid, ref role);

            Assert.IsFalse(role.Id == 0, "Role id not assigned");
            Assert.IsTrue(role.Name == "PMO", "Name is not stored correctly");

            role.Name = "Project Management Organization";
            _service.UpdateRole(_projectid, ref role);

            Assert.IsTrue(role.Name == "Project Management Organization", "Name is not stored correctly");

            _service.DeleteRole(_projectid, role.Id);
        }


        [TestMethod]
        public void AgileZenService_Can_Get_All_Tags_By_ProjectId()
        {
            var tags = _service.GetTags(_projectid);

            Assert.IsNotNull(tags, "GetProjects did not return a result");
            Assert.IsFalse(tags.Page == 0, "GetProjects did return a result, but no Page");
            Assert.IsFalse(tags.PageSize == 0, "GetProjects did return a result, but no PageSize");
            Assert.IsFalse(tags.TotalItems == 0, "GetProjects did return a result, but no TotalItems");
            Assert.IsFalse(tags.TotalPages == 0, "GetProjects did return a result, but no TotalPages");
            Assert.IsFalse(tags.Items.Count() == 0, "GetProjects did return a result, but no projects");
        }
    }
}
